<!--
 * @Author: Dorange.Wang
 * @Date: 2021-07-28 11:08:21
 * @LastEditors: your name
 * @LastEditTime: 2021-10-27 15:50:41
 * @Description: file content
-->
<template>
  <el-dialog :visible="visible" center :show-close="false" width="420px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>默认排序</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="visible = false"
        ></span>
      </div>
    </div>
    <el-form
      ref="form"
      class="pl30 pr30"
      :model="form"
      label-width="100px"
      size="small"
    >
      <el-form-item
        label="数据字段"
        prop="fieldCode"
        :rules="[{ required: true, message: '请选择字段', trigger: 'change' }]"
      >
        <el-select  style="width: 200px" v-model="form.fieldCode" placeholder="请选择">
          <el-option
            v-for="item in options.fields"
            :key="item.code"
            :label="item.name"
            :value="item.code"
            :disabled="item.disabled"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="排序规则"
        prop="orderRule"
        :rules="[
          { required: true, message: '请选择排序规则', trigger: 'change' },
        ]"
      >
        <el-select style="width: 200px" v-model="form.orderRule" placeholder="请选择">
          <el-option
            v-for="item in odrerRules"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleDialogCancel">取消</el-button>
      <el-button type="primary" @click="handleDialogConfirm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
const DEFAULT_OPTIONS = {
  fields: [],
  onConfirm: null,
};
export default {
  name: "tableDefaultOrderModal",
  data() {
    return {
      visible: false,
      form: {},
      options: {
        ...DEFAULT_OPTIONS,
      },
      odrerRules: [
        {
          code: "asc",
          name: "升序",
        },
        { code: "desc", name: "降序" },
      ],
    };
  },

  computed: {},

  methods: {
    async init(options) {
      this.options = {
        ...DEFAULT_OPTIONS,
        ...options,
      };
      this.form = {
        fieldCode: "",
        orderRule: "",
        ...this.options.data,
      };
      this.visible = true;
      await this.$nextTick();
      this.$refs.form.clearValidate();
    },
    async handleDialogConfirm() {
      await this.$refs.form.validate();
      this.options.onConfirm && this.options.onConfirm(this.form);
      this.visible = false;
    },
    handleClosed() {
      this.form = {};
      this.$refs.form.resetFields();
    },
    handleDialogCancel() {
      this.visible = false;
    },
  },
};
</script>
